package edu.whu.demo.service;

import edu.whu.demo.entity.Item;
import edu.whu.demo.entity.Supplier;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SupplierService {
    //map，非mybatis写法
    private Map<Long, Supplier> suppliers =
            Collections.synchronizedMap(new HashMap<Long, Supplier>());

    public Supplier addSupplier(Supplier supplier) {
        suppliers.put(supplier.getId(), supplier);
        return supplier;
    }

    public Supplier getSupplier(long id) {
        return suppliers.get(id);
    }

    public List<Supplier> findSuppliers(String name) {
        List<Supplier> result=new ArrayList<>();
        for (Supplier supplier: suppliers.values()){
            if (name!=null && !supplier.getName().contains(name)) {
                continue;
            }
            result.add(supplier);
        }
        return result;
    }

    public void updateSupplier(long id, Supplier supplier) {
        Supplier supplier1  = suppliers.get(id);
        supplier1.setName(supplier.getName());
        suppliers.put(id, supplier1);
    }

    public void deleteSupplier(long id) {
        suppliers.remove(id);
    }

    public void deleteAll(){
        suppliers.clear();
    }
}
